<template>
	<view class="container">
		<view class="form-box">
			<!-- 任务描述 -->
			<view class="task-section">
				<view class="form-item">
					<text class="label">任务类型</text>
					<picker mode="selector" :range="taskTypes" @change="onTaskTypeChange">
						<view class="picker">
							<text>{{formData.taskType || '请选择任务类型'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">任务描述</text>
					<textarea 
						v-model="formData.description" 
						placeholder="请详细描述您的需求，如：帮我到一食堂取外卖，取餐码12345" 
						:maxlength="200"
					/>
					<text class="word-count">{{formData.description.length}}/200</text>
				</view>

				<!-- 图片上传 -->
				<view class="form-item">
					<text class="label">相关图片（选填）</text>
					<view class="image-uploader">
						<view class="image-list" v-if="formData.images.length > 0">
							<view class="image-item" v-for="(image, index) in formData.images" :key="index">
								<image :src="image" mode="aspectFill"></image>
								<text class="delete-btn" @tap="deleteImage(index)">×</text>
							</view>
						</view>
						<view class="upload-btn" @tap="chooseImage" v-if="formData.images.length < 3">
							<uni-icons type="camera" size="32" color="#999"></uni-icons>
							<text>上传图片</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 地点信息 -->
			<view class="location-section">
				<view class="form-item">
					<text class="label">取货地点</text>
					<view class="location-select" @tap="selectPickupLocation">
						<text>{{formData.pickupLocation || '请选择取货地点'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="form-item">
					<text class="label">送达地点</text>
					<view class="location-select" @tap="selectDeliveryLocation">
						<text>{{formData.deliveryLocation || '请选择送达地点'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="form-item">
					<text class="label">期望送达时间</text>
					<picker mode="time" :value="formData.expectedTime" @change="onTimeChange">
						<view class="picker">
							<text>{{formData.expectedTime || '请选择时间'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>
			</view>

			<!-- 费用信息 -->
			<view class="price-section">
				<view class="form-item">
					<text class="label">物品金额（如需代付）</text>
					<input 
						type="digit" 
						v-model="formData.itemPrice" 
						placeholder="请输入物品金额（选填）" 
						class="price-input"
					/>
				</view>

				<view class="price-item">
					<text class="label">跑腿费</text>
					<text class="price">¥{{serviceFee}}</text>
				</view>
				<view class="price-item">
					<text class="label">加急服务</text>
					<switch :checked="formData.isUrgent" @change="onUrgentChange" color="#2979ff" />
				</view>
				<view class="price-item total">
					<text class="label">总计</text>
					<text class="price">¥{{totalPrice}}</text>
				</view>
			</view>
		</view>

		<!-- 发布按钮 -->
		<view class="submit-btn" @tap="submitOrder">
			发布订单
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			taskTypes: ['取外卖', '买药', '其他跑腿', '代办事务'],
			formData: {
				taskType: '',
				description: '',
				images: [],
				pickupLocation: '',
				deliveryLocation: '',
				expectedTime: '',
				itemPrice: '',
				isUrgent: false
			},
			serviceFee: 5,
			urgentFee: 5
		}
	},
	computed: {
		totalPrice() {
			const itemPrice = parseFloat(this.formData.itemPrice) || 0
			return (itemPrice + 
				this.serviceFee + 
				(this.formData.isUrgent ? this.urgentFee : 0)
			).toFixed(2)
		}
	},
	methods: {
		onTaskTypeChange(e) {
			this.formData.taskType = this.taskTypes[e.detail.value]
		},
		chooseImage() {
			uni.chooseImage({
				count: 1,
				success: (res) => {
					this.formData.images.push(res.tempFilePaths[0])
				}
			})
		},
		deleteImage(index) {
			this.formData.images.splice(index, 1)
		},
		selectPickupLocation() {
			uni.showActionSheet({
				itemList: ['一食堂', '二食堂', '三食堂', '校医院', '快递点', '其他'],
				success: (res) => {
					this.formData.pickupLocation = ['一食堂', '二食堂', '三食堂', '校医院', '快递点', '其他'][res.tapIndex]
				}
			})
		},
		selectDeliveryLocation() {
			uni.showActionSheet({
				itemList: ['宿舍楼', '教学楼', '图书馆', '食堂'],
				success: (res) => {
					this.formData.deliveryLocation = ['宿舍楼', '教学楼', '图书馆', '食堂'][res.tapIndex]
				}
			})
		},
		onTimeChange(e) {
			this.formData.expectedTime = e.detail.value
		},
		onUrgentChange(e) {
			this.formData.isUrgent = e.detail.value
		},
		submitOrder() {
			// 表单验证
			if (!this.formData.taskType) {
				uni.showToast({
					title: '请选择任务类型',
					icon: 'none'
				})
				return
			}
			if (!this.formData.description) {
				uni.showToast({
					title: '请填写任务描述',
					icon: 'none'
				})
				return
			}
			if (!this.formData.pickupLocation) {
				uni.showToast({
					title: '请选择取货地点',
					icon: 'none'
				})
				return
			}
			if (!this.formData.deliveryLocation) {
				uni.showToast({
					title: '请选择送达地点',
					icon: 'none'
				})
				return
			}
			if (!this.formData.expectedTime) {
				uni.showToast({
					title: '请选择期望送达时间',
					icon: 'none'
				})
				return
			}

			// TODO: 发送请求保存订单
			uni.showLoading({
				title: '发布中...'
			})

			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form-box {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
}

.task-section, .location-section {
	margin-bottom: 30rpx;
}

.form-item {
	margin-bottom: 30rpx;
	position: relative;

	.label {
		font-size: 28rpx;
		color: $text-color;
		margin-bottom: 20rpx;
		display: block;
	}

	.picker, .location-select {
		width: 100%;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: $text-color;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	textarea {
		width: 100%;
		height: 200rpx;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
	}

	.word-count {
		position: absolute;
		right: 20rpx;
		bottom: 20rpx;
		font-size: 24rpx;
		color: #999;
	}

	.price-input {
		width: 100%;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
	}
}

.image-uploader {
	.image-list {
		display: flex;
		flex-wrap: wrap;
		margin-bottom: 20rpx;

		.image-item {
			width: 200rpx;
			height: 200rpx;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
			}

			.delete-btn {
				position: absolute;
				top: -20rpx;
				right: -20rpx;
				width: 40rpx;
				height: 40rpx;
				background-color: rgba(0, 0, 0, 0.5);
				color: #fff;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 32rpx;
			}
		}
	}

	.upload-btn {
		width: 200rpx;
		height: 200rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		text {
			font-size: 24rpx;
			color: #999;
			margin-top: 10rpx;
		}
	}
}

.price-section {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;

	.price-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		.label {
			font-size: 28rpx;
			color: $text-color;
		}

		.price {
			font-size: 32rpx;
			color: #ff5500;
			font-weight: bold;
		}

		&.total {
			margin-top: 30rpx;
			padding-top: 20rpx;
			border-top: 1rpx solid $border-color;
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: $theme-color;
	color: #fff;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style> 